package com.example.spring.demo.controller;

import com.example.spring.demo.service.HelloService;
import com.example.spring.framework.annotation.Autowired;
import com.example.spring.framework.annotation.Controller;
import com.example.spring.framework.annotation.RequestMapping;
import com.example.spring.framework.annotation.RequestParam;
import com.example.spring.framework.webmvc.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname HelloController
 * @Date 2019/5/7 21:26
 * @Author admin
 * @Description TODO
 */
@Controller
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello.json")
    public void helloJson(HttpServletRequest request, HttpServletResponse response,
                      @RequestParam("name") String name) {
        String result = helloService.hello(name);
        this.out(response, result);
    }

    @RequestMapping("/hello.view")
    public ModelAndView helloView(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam("name") String name) {
        return new ModelAndView(name);
    }

    private ModelAndView out(HttpServletResponse response, String str) {
        try {
            response.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
